package com.cch.demo.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cch.demo.bus.domain.Customer;
import com.cch.demo.bus.domain.Goods;
import com.cch.demo.bus.domain.Sales;
import com.cch.demo.bus.service.CustomerService;
import com.cch.demo.bus.service.GoodsService;
import com.cch.demo.bus.service.SalesService;
import com.cch.demo.bus.vo.SalesVo;
import com.cch.demo.bus.vo.SalesVo;
import com.cch.demo.bus.vo.SalesVo;
import com.cch.demo.sys.domain.User;
import com.cch.demo.sys.util.DataGridView;
import com.cch.demo.sys.util.ResultObj;
import com.cch.demo.sys.util.WebUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 华哥
 * @since 2020-03-12
 */
@RestController
@RequestMapping("/sales")
public class SalesController {

    @Resource
    private GoodsService goodsServiceImpl;

    @Resource
    private CustomerService customerServiceImpl;

    @Resource
    private SalesService salesServiceImpl;

    @RequestMapping("/loadAllSales")
    public DataGridView loadAllSales(SalesVo salesVo){
        QueryWrapper<Sales> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(salesVo.getCustomerid()!=null&&salesVo.getCustomerid()!=0,"customerid",salesVo.getCustomerid());
        queryWrapper.eq(salesVo.getGoodsid()!=null&&salesVo.getGoodsid()!=0,"goodsid",salesVo.getGoodsid());
        queryWrapper.ge(salesVo.getStartTime()!=null, "salestime", salesVo.getStartTime());
        queryWrapper.le(salesVo.getEndTime()!=null, "salestime", salesVo.getEndTime());
        queryWrapper.like(StringUtils.isNotBlank(salesVo.getOperateperson()), "operateperson", salesVo.getOperateperson());
        queryWrapper.like(StringUtils.isNotBlank(salesVo.getRemark()), "remark", salesVo.getRemark());
        queryWrapper.orderByDesc("salestime");

        IPage<Sales> page = new Page<>(salesVo.getPage(),salesVo.getLimit());
        salesServiceImpl.page(page,queryWrapper);

        //给sales中供应商添加名字
        List<Sales> saless  = page.getRecords();
        for(Sales sales:saless){

            Customer customer =  customerServiceImpl.getById(sales.getCustomerid());
            if(null!=customer){
                sales.setCustomername(customer.getCustomername());
            }

            Goods goods = goodsServiceImpl.getById(sales.getGoodsid());
            if(null!=goods){
                sales.setGoodsname(goods.getGoodsname());
                sales.setSize(goods.getSize());
            }
        }
        return new DataGridView(page.getTotal(),saless);
    }

    @RequestMapping("addSales")
    public ResultObj addSales(SalesVo salesVo){
        try{

            salesVo.setSalestime(new Date());
            User user=(User) WebUtils.getSession().getAttribute("user");
            salesVo.setOperateperson(user.getName());
            salesServiceImpl.save(salesVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改
     */
    @RequestMapping("updateSales")
    public ResultObj updateSales(SalesVo salesVo) {
        try {
            salesServiceImpl.updateById(salesVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     */
    @RequestMapping("deleteSales")
    public ResultObj deleteSales(Integer id) {
        try {
            salesServiceImpl.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}

